/**
 * related to followRecord.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var nf = sm("do_Notification");
var app = sm("do_App");
var page = sm("do_Page");
var Controller = require("core/controller");
var log = require("util/log")
var navigate = require("util/navigate")
var router = require("config/router")
var net = require("util/net");
var server = require('config/server')
var cf_lv = ui("cf_lv");
var btn_addRecord = ui("btn_addRecord")
var filter = require('util/filter')
var common = require('util/common')
var paging = require('util/paging')
Controller({
	name : 'index',
	mapping : {
		cf_lv:"data2",
//		"layout_follow.bgImage":"layout_follow"
	},
	data : {
		paging:paging.create(),
		"data2" : [{
			"createTime" : "",
			"fr_time" : "",
			"follTarget" : "",
			"follName" : "",
			"follFeedback" : "",
			"follPhoneNum" : "",
			"fr_smallBow":"",
//			"layout_follow":"source://image/app/blank.png"
		}],
		lastShowIndex:0,
		cupoCasenum:page.getData().cupoCasenum,
		flowDetails:[]
	},
	ui:{
		layout_follow:'layout_follow',
		cf_lv:"cf_lv"
	},
	event : {
		'btn_addRecord':{
			'touch':function(){
				this.openAddRecord(this.data.dataFollow)
			}
		},
		'cf_lv' : {
		'touch' : function(index) {
// 获取点击list的数据并发送到下一级页面
//			log.info(45454545454545,)
			this.toFollowInfo('cf_lv', this.data.flowDetails[index]);
		},
		'scroll' : function(index) {
			log.info(index)
			// 判断是否需要修改状态点颜色
//			this.data.data2[0].fr_smallBow="#0091DCFF"
//				log.info(6655544554544,this.data.data2[0])
//				this.refresh('cf_lv')
			if(this.data.lastShowIndex===index.firstVisiblePosition){
				return
			}else{
				this.data.lastShowIndex  = index.firstVisiblePosition
			}
			
			// 更新当前节点颜色
			this.data.data2.each(function(index,item){
				var targetColor = index===this.data.lastShowIndex?"#0091DCFF":"#E4E4E4FF"
				if(this.data.data2[index].fr_smallBow!==targetColor){
					this.data.data2[index].fr_smallBow = targetColor
				}
				
			},this)
			this.refresh('cf_lv')
		},
		/**
         * 列表下拉刷新
         */
        'pull':function(data){
            if(data.state == 2){
                this.data.messdata = []
                this.data.paging.reset()
                this.getData3()
            }
        },
        /**
         * 列表上拉更新
         */
        'push':function(data){
            if(data.state == 2){
                if(this.data.paging.next()){
                	this.getData3()
                }else{
                    this.ui.cf_lv.rebound()
                }
            }
        }
	}
	},
	methods : {
		getData3: function () {
			return net.send({
				server:server.organization.getFllowRecord,
				pag:this.data.paging,
				data:{
					cupoCasenum:this.data.dataFollow.current.cupoCasenum,
					sort: "createTime,desc"
				},
				success:function(followdata){
					log.info(65554,followdata)
					this.data.flowDetails=followdata.content
					var list = followdata.content.map(function(x) {
						var flAddress=filter.getCityName(x.follProvid) + filter.getCityName(x.follCityid) + filter.getCityName(x.follAreaid)+x.follPhoneNum
						return {
							createTime : filter.dateConvert(x.createTime),
							follTarget :  filter.typeCodeConvert(x.follTarget),
							follName : x.follCust,
							follFeedback : filter.typeCodeConvert(x.follFeedback),
							follPhoneNum : flAddress||'',
							fr_time : common.dateFormat(x.createTime,'hh:mm'),
							fr_smallBow:"#E4E4E4FF"
						}
					})
					for (var i = 0; i < list.length; i++) {
						list[i].showTitle = !(i!=0&&list[i-1].createTime===list[i].createTime)
						if(i===0){
							list[i].fr_smallBow ="#0091DCFF"
						}
					}
					this.data.data2 = list
					if (this.data.data2.length==0) {
						this.ui.layout_follow.bgImage="source://image/app/blank.png"
					}
					this.refresh('cf_lv')
				},
				error: function(data){
					log.info(5457,data)
				},
				callback:function(){
                    this.ui.cf_lv.rebound()
                },
				scope:this
			})
		},
		openAddRecord:function(folred){
			navigate.redirectTo({
				view:router.follow.addRecord,
				data:{
					folred:folred.current
				},
				back:function(data){
					this.ui.layout_follow.bgImage="source://image/app/nullbg.png"
					this.getData3()
				},
				scope:this
			})
		},
		toFollowInfo : function(id, data) {
			navigate.redirectTo({
				view : router.follow.followDetailInfo,
				data : {
					current: data
				},
				back : function(data) {
					232323
				}
			})
		}
	},
	loaded : function(data) {
		this.data.dataFollow=data	
		this.loadReady([this.getData3()])
	}
})